<template>
  <view class="content">
    <van-nav-bar title="优惠券" left-text="" :left-arrow="true" placeholder fixed @click-left="onClickLeft" />
    <view class="coupon">
      <view class="coupon-tab">
        <view class="tab-box">
          <text :class="listQuery.status === 1 ? 'active' : ''" @click="changeTab(1)">待使用</text>
          <text :class="listQuery.status === 2 ? 'active' : ''" @click="changeTab(2)">已使用</text>
          <text :class="listQuery.status === 3 ? 'active' : ''" @click="changeTab(3)">已过期</text>
        </view>
      </view>
      <Scrollbox @lowerFun="getList" :status="loadingStatus">
        <template #contScrollBox>
          <view class="coupon-box">
            <view class="coupon-box-list" v-for="(item, index) in list" :key="index">
              <view class="coupon-option">
                <van-checkbox shape="round" checked-color="#3775F2" icon-size="48rpx"></van-checkbox>
              </view>
              <view
                class="coupon-item"
                :class="listQuery.status === 2 ? 'coupon-use' : listQuery.status === 3 ? 'coupon-expire' : ''"
              >
                <view class="item-box">
                  <view class="coupon-price">
                    <text>￥{{ item.coupon_money }}</text>
                  </view>
                  <view class="coupon-info">
                    <view class="info-price">{{ item.coupon_name }}</view>
                    <view class="info-date">{{ item.start_time }} - {{ item.end_time }}</view>
                  </view>
                  <view class="info-btn">x&nbsp;{{ item.coupon_num }}</view>
                </view>
              </view>
            </view>
          </view>
        </template>
      </Scrollbox>
    </view>
  </view>
</template>

<script>
import { reactive, toRefs } from 'vue'
import Scrollbox from '@/components/scrollbox/index'
import { onShow } from '@dcloudio/uni-app'
import { coupons } from '@/http/user'
export default {
  components: {
    Scrollbox,
  },
  setup() {
    const state = reactive({
      list: [],
      listQuery: {
        status: 1,
        page: 1,
      },
      loadingStatus: 'loading',
      isEnd: false,
    })
    const onClickLeft = () => {
      uni.navigateBack({
        delta: 1,
      })
    }
    const getList = async () => {
      if (state.isEnd) return
      try {
        let data = await coupons(state.listQuery)
        const { list, total_page } = data.data
        state.list = state.listQuery.page === 1 ? list : state.list.concat(list)

        if (list.length === 0 || total_page === state.listQuery.page) {
          state.loadingStatus = 'loadmore'
          state.isEnd = true
        } else {
          state.loadingStatus = 'loading'
          state.listQuery.page++
        }
      } catch (e) {
        state.isRefresh = false
        state.loading = false
      }
    }
    const changeTab = (num) => {
      state.listQuery.status = num
      state.listQuery.page = 1
      state.isEnd = false
      getList()
    }
    onShow(() => {
      state.listQuery.status = 1
      state.listQuery.page = 1
      state.isEnd = false
      getList()
    })
    return {
      ...toRefs(state),
      getList,
      onClickLeft,
      changeTab,
    }
  },
}
</script>

<style lang="scss">
.content {
  height: 100vh;
}
.coupon-tab {
  width: calc(100% - 48rpx);
  display: flex;
  justify-content: space-between;
  color: #6c7385;
  padding: 0 24rpx;
  background: #fff;
  font-size: 28rpx;
  .tab-box {
    display: flex;
    margin-left: -24rpx;
    text {
      position: relative;
      display: block;
      min-width: 112rpx;
      height: 96rpx;
      padding: 0 24rpx;
      line-height: 96rpx;
      text-align: center;
      &.active {
        font-weight: 500;
        color: #3775f2;
        &::before {
          content: '';
          display: block;
          height: 4rpx;
          width: calc(100% - 48rpx);
          background: #3775f2;
          position: absolute;
          bottom: 0;
          left: 24rpx;
        }
      }
    }
  }
  .tab-del {
    height: 96rpx;
    padding: 0 24rpx;
    line-height: 96rpx;
  }
}
.coupon-box-list {
  padding: 24rpx;
  width: calc(100% + 44rpx);
  display: flex;
  align-items: center;
  justify-content: space-between;
  transform: translateX(-88rpx);
  transition: 1s;
  .coupon-option {
    width: 112rpx;
  }
}
.coupon-item {
  position: relative;
  width: calc(100% - 48rpx);
  padding: 32rpx;
  padding-left: 64rpx;
  border-radius: 16rpx;
  background: #e94148;
  overflow: hidden;
  &::before {
    content: '';
    display: block;
    width: 100%;
    height: calc(100% - 10rpx);
    background: #fff;
    position: absolute;
    z-index: 1;
    top: 10rpx;
    left: 0;
    border-radius: 16rpx;
  }
  .item-box {
    position: relative;
    z-index: 3;
    display: flex;
    justify-content: space-between;
    .coupon-price {
      color: #e94148;
      text {
        font-size: 64rpx;
      }
      .price-tag {
        margin-top: 10rpx;
        font-size: 20rpx;
        line-height: 48rpx;
        height: 48rpx;
        padding: 0 16rpx;
        background: #fff0f0;
        border-radius: 24rpx;
        text-align: center;
      }
    }
    .coupon-info {
      padding: 0 24rpx 0 58rpx;
      color: #2b303b;
      .info-price {
        font-size: 36rpx;
      }
      .info-des {
        font-size: 28rpx;
        margin: 16rpx 0;
      }
      .info-date {
        font-size: 24rpx;
        color: #7f8798;
      }
    }
    .info-btn {
      line-height: 48rpx;
      font-size: 32rpx;
      font-weight: bold;
      text-align: center;
    }
  }
}
.coupon-box-list {
  &.active {
    transform: translateX(0rpx);
  }
}
.coupon-use {
  background: #7f8798;
  &:after {
    content: '';
    display: block;
    width: 120rpx;
    height: 120rpx;
    background: url('https://images.tongzhuangkc.com/tz/icon-used.png') no-repeat;
    background-size: 100% 100%;
    position: absolute;
    top: 0;
    right: 0;
    z-index: 2;
  }
  .item-box {
    color: #555b66 !important;
    .coupon-price {
      color: #7f8798;
      .price-tag {
        background: #f5f6fa;
      }
    }
    .coupon-info {
      .info-date {
        color: #7f8798;
      }
    }
  }
}
.coupon-expire {
  &:after {
    background: url('https://images.tongzhuangkc.com/tz/icon-guoqi.png') no-repeat;
    background-size: 100% 100%;
  }
}
</style>
